package com.idlecode.panel.tool.cron;

import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.ui.ComboBox;

import javax.swing.*;
import java.awt.*;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-11-07 15:00
 **/
public class YearPanel extends JPanel {

    private JTextField yearTField;

    private JRadioButton perYearRadioBtn; //每年

    private JRadioButton notDesignateRadioBtn; //不指定

    private JRadioButton cycleRadioBtn; //周期
    private ComboBox<String> cycleStartComboBox;
    private ComboBox<String> cycleEndComboBox;

    public YearPanel(JTextField yearTField) {
        this.yearTField = yearTField;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //每年
        JPanel perYearPanel = new JPanel();
        perYearPanel.setLayout(new BoxLayout(perYearPanel, BoxLayout.X_AXIS));
        perYearRadioBtn = new JRadioButton("每年", false);
        perYearRadioBtn.setFont(perYearRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        perYearPanel.add(perYearRadioBtn);
        perYearPanel.add(Box.createRigidArea(new Dimension(5, 0)));
        perYearPanel.add(new JLabel("允许的通配符[, - * /] 非必填"));
        perYearPanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(perYearPanel);

        perYearRadioBtn.addActionListener(e -> {
            if (perYearRadioBtn.isSelected()) {
                yearTField.setText("*");
            }
        });
        //不指定
        JPanel notDesignatePanel = new JPanel();
        notDesignatePanel.setLayout(new BoxLayout(notDesignatePanel, BoxLayout.X_AXIS));
        notDesignateRadioBtn = new JRadioButton("不指定", true);
        notDesignateRadioBtn.setFont(notDesignateRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        notDesignatePanel.add(notDesignateRadioBtn);
        notDesignatePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(notDesignatePanel);

        notDesignateRadioBtn.addActionListener(e -> {
            if (notDesignateRadioBtn.isSelected()) {
                yearTField.setText("");
            }
        });
        //周期
        JPanel cyclePanel = new JPanel();
        cyclePanel.setLayout(new BoxLayout(cyclePanel, BoxLayout.X_AXIS));
        cycleRadioBtn = new JRadioButton("周期", false);
        cycleRadioBtn.setFont(cycleRadioBtn.getFont().deriveFont(Font.BOLD, 13));
        cyclePanel.add(cycleRadioBtn);
        cyclePanel.add(Box.createRigidArea(new Dimension(5, 0)));

        cyclePanel.add(new JLabel("从"));

        String[] cycleStartOptions = {"2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035",
                "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050",
                "2051", "2052", "2053", "2054", "2055", "2056", "2057", "2058", "2059", "2060", "2061", "2062", "2063", "2064", "2065",
                "2066", "2067", "2068", "2069", "2070", "2071", "2072", "2073", "2074", "2075", "2076", "2077", "2078", "2079", "2080",
                "2081", "2082", "2083", "2084", "2085", "2086", "2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094", "2095",
                "2096", "2097", "2098", "2099", "2100"};
        cycleStartComboBox = new ComboBox<>(cycleStartOptions);
        cycleStartComboBox.setMaximumSize(new Dimension(50, 30));
        cyclePanel.add(cycleStartComboBox);
        cycleStartComboBox.addActionListener(e -> {
            if (!cycleRadioBtn.isSelected()) {
                cycleRadioBtn.setSelected(true);
            }
            yearTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
        });

        cyclePanel.add(new JLabel("-"));

        String[] cycleEndOptions = {"2024", "2025", "2026", "2027", "2028", "2029", "2030", "2031", "2032", "2033", "2034", "2035",
                "2036", "2037", "2038", "2039", "2040", "2041", "2042", "2043", "2044", "2045", "2046", "2047", "2048", "2049", "2050",
                "2051", "2052", "2053", "2054", "2055", "2056", "2057", "2058", "2059", "2060", "2061", "2062", "2063", "2064", "2065",
                "2066", "2067", "2068", "2069", "2070", "2071", "2072", "2073", "2074", "2075", "2076", "2077", "2078", "2079", "2080",
                "2081", "2082", "2083", "2084", "2085", "2086", "2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094", "2095",
                "2096", "2097", "2098", "2099", "2100"};
        cycleEndComboBox = new ComboBox<>(cycleEndOptions);
        cycleEndComboBox.setMaximumSize(new Dimension(50, 30));
        cyclePanel.add(cycleEndComboBox);
        cycleEndComboBox.addActionListener(e -> {
            if (!cycleRadioBtn.isSelected()) {
                cycleRadioBtn.setSelected(true);
            }
            yearTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
        });

        cyclePanel.add(new JLabel("年"));

        cyclePanel.add(Box.createHorizontalGlue()); // 右侧弹性空间
        this.add(cyclePanel);

        cycleRadioBtn.addActionListener(e -> {
            if (cycleRadioBtn.isSelected()) {
                yearTField.setText(cycleStartComboBox.getSelectedItem() + "-" + cycleEndComboBox.getSelectedItem());
            }
        });

        ButtonGroup group = new ButtonGroup();
        group.add(perYearRadioBtn);
        group.add(notDesignateRadioBtn);
        group.add(cycleRadioBtn);
    }

    public void inverseParseUi(String expression) {
        if (StrUtil.isBlank(expression)) {
            notDesignateRadioBtn.setSelected(true);
            return;
        }
        yearTField.setText(expression);
        if ("*".equals(expression)) {
            perYearRadioBtn.setSelected(true);
            return;
        }
        if (expression.contains("-")) {
            cycleRadioBtn.setSelected(true);
            String[] cycleCondition = expression.split("-");
            for (int i = 0; i < cycleCondition.length; i++) {
                String condition = cycleCondition[i];
                switch (i) {
                    case 0:
                        cycleStartComboBox.setSelectedItem(condition);
                    case 1:
                        cycleEndComboBox.setSelectedItem(condition);
                    default:
                        break;
                }
            }
            return;
        }
        notDesignateRadioBtn.setSelected(true);
    }
}
